/**
 * All files in the shared package will be used by both the server and the client.
 * These will be copied to both projects when deploying.
 * 
 * Belongs to package: core
 */
package alpaca.core;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * The user class for ORMLite. Includes full name, email and password.
 * 
 * @see alpaca.database.ORMLiteDatabaseSystem 
 */
@DatabaseTable(tableName = "users")
public class ORMLiteUser implements IUser {
	
	@DatabaseField(generatedId = true)
	private int id;
	
	@DatabaseField
	private String firstName;
	@DatabaseField
	private String lastName;
	
	@DatabaseField(unique = true)
	private String email;
	@DatabaseField(canBeNull = false)
	private String password;
	
	@DatabaseField
	private boolean active;
	
	private boolean online;

	@Deprecated
	public ORMLiteUser() {
		
	}

	public ORMLiteUser(String firstName, String lastName, String email, String password) {
		this(firstName, lastName, email, password, false);
	}
	
	/**
	 * Creates a User.<br>
	 * Throws {@link IllegalArgumentException} if email is null.
	 * @param firstName the first name of the user.
	 * @param lastName the last name of the user.
	 * @param email the email of the user, cannot be null.
	 * @param password the password of the user.
	 */
	public ORMLiteUser(String firstName, String lastName, String email, String password, boolean status) {
		if(email == null) {
			throw new IllegalArgumentException("email can not be null");
		}
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.password = password;
		setOnline(status);
		this.active = true;
	}
	
	public int getId() {
		return id;
	}
	
	@Override
	public String getFullName() {
		return lastName + ", " + firstName;
	}
	
	@Override
	public String getFirstName() {
		return firstName;
	}

	@Override
	public String getLastName() {
		return lastName;
	}

	@Override
	public String getEmail() {
		return email;
	}
	
	@Override
	public boolean isActive() {
		return active;
	}
	
	@Override
	public boolean isOnline() {
		return online;
	}

	@Override
	public void setOnline(boolean online) {
		this.online = online;
	}

	@Override
	public String toString(){
		String string = (active)?"":"Inactive: ";
		return string + getFullName() + " - " + email;
	}
	
	/**
	 * A User is equal to another User if the email-addresses are the same.
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj != null && obj instanceof ORMLiteUser) {
			ORMLiteUser anotherUser = (ORMLiteUser) obj;
			return anotherUser.getEmail().equals(this.getEmail());
		}	
		return false;
	}
	
	/**
	 * The hashCode of a User is the hashCode of the User's email-address (String).
	 */
	@Override
	public int hashCode() {
		return email.hashCode();
	}

}
